package com.lucian.ge.service.stockinfo.controller;

import com.alibaba.fastjson.JSONObject;
import com.lucian.ge.comman.model.ResultMap;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;


import java.util.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * Created by Lucicol on 2019/5/8.
 */
@Api(value = "IndexController", description = "股市行情")
@RestController
@RequestMapping("/stock_index")
public class IndexController {
    @Value("${com.stockURL}")
    private String stockURL;
    private final ResultMap resultMap;

    @Autowired
    public IndexController( ResultMap resultMap) {
        this.resultMap = resultMap;
    }

    @GetMapping("/test")
    public void test() {

        String s = "[{\"id\":\"17051801\",\"name\":\"lucy\"},{\"id\":\"17051802\",\"name\":\"peter\"}]";
        JSONArray array = JSON.parseArray(s);
        for (int i = 0; i < array.size(); i++) {
            //JSONArray中的数据转换为String类型需要在外边加"";不然会报出类型强转异常！
            String str = array.get(i)+"";
            JSONObject object = JSON.parseObject(str);
            System.out.println(object.get("name"));
        }

    }

    @ApiOperation(value="当前指数", notes="")
    @GetMapping("/now_index")
    public ResultMap getNowIndex() {
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject(stockURL + "/now_index", String.class);
//        System.out.println(JSON.parseArray(result));
        return resultMap.success().code(200).message("更多指数").data(JSON.parseArray(result));
    }

    @ApiOperation(value="更多指数", notes="")
    @GetMapping("/more_index")
    public ResultMap getMoreIndex() {
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject(stockURL + "/stock_index", String.class);
        return resultMap.success().code(200).message("更多指数").data(JSON.parseArray(result));
    }
}
